package fileupload

import (
	"fmt"
	"ginjt/JsonResult"
	"ginjt/amazons3utils"
	"git.yjqw.com/lmh/go-utils/did"
	"github.com/gin-gonic/gin"
	"log"
	"net/http"
	"strconv"
)


var Bucket = "gotest1"
func FilePathPre() string  {
	return "2020/09/"
}

func UploadFile(c *gin.Context) {
	//curl -X POST http://localhost:8080/upload \
	//-F "file=@/Users/appleboy/test.zip" \
	//-H "Content-Type: multipart/form-data"
	// 单文件

	file, _ := c.FormFile("files")
	log.Println(file.Filename)



	// 上传文件到指定的路径
	filePath:=strconv.FormatInt(did.Next(),10)+"_"+file.Filename
	// c.SaveUploadedFile(file, "files"+"/"+filePath)
	amazons3utils.UploadingFile("gotest1",filePath,file,amazons3utils.GetSession())

	c.String(http.StatusOK, fmt.Sprintf("'%s' uploaded!", file.Filename))
}


func UploadFiles(c *gin.Context) {
	// 多文件
	form, _ := c.MultipartForm()
	fmt.Println(form)
	files := form.File["files"]

	f := NewFileOject()


	for _, file := range files {
		//log.Println(file.Filename)

		// 上传文件到指定的路径
		// c.SaveUploadedFile(file, dst)
		//c.SaveUploadedFile(file, "files"+"/"+strconv.FormatInt(did.Next(),10)+"_"+file.Filename)

		//TODO
		//路径自定义，文件名的规范
		filePath:=FilePathPre()+strconv.FormatInt(did.Next(),10)+"_"+file.Filename
		//filePath +=
		if amazons3utils.UploadingFile(Bucket,filePath,file,amazons3utils.GetSession()) {
			f.OkCount++
			f.PutOk(file.Filename,filePath)
		} else {
			f.FailCount++
			f.PutFail(file.Filename,filePath)
		}

	}
	//c.String(http.StatusOK, fmt.Sprintf("%d files uploaded!", len(files)))
	c.JSON(http.StatusOK,JsonResult.Ok().PutData(f).GetH())
}